import 'package:flutter/material.dart';
import 'package:flutter_demo/chapter6-Roll/cache/KeepAliveWrapper.dart';

void main() {
  runApp(const MaterialApp(
    home: TabBarViewWidget(),
  ));
}

class TabBarViewWidget extends StatefulWidget {
  const TabBarViewWidget({Key? key}) : super(key: key);

  @override
  _TabBarViewWidgetState createState() => _TabBarViewWidgetState();

}

class _TabBarViewWidgetState extends State<TabBarViewWidget>
    with SingleTickerProviderStateMixin {


  late TabController _tabController;
  List tabs = ["新闻", "历史", "图片"];

  @override
  void initState() {
    super.initState();
    _tabController = TabController(length: tabs.length, vsync: this);
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: const Text("App Name"),
        bottom: TabBar(
          controller: _tabController,
          tabs: tabs.map((e) => Tab(text: e,)).toList(),
        ),
      ),
      body: TabBarView(
          controller: _tabController,
          children: tabs.map((e) {
            return KeepAliveWrapper(
              child: Container(
                alignment: Alignment.center,
                child: Text(e, textScaleFactor: 5,),
              ),
            );
          }).toList()
      ),
    );
  }

  @override
  void dispose() {
    //  释放资源
    _tabController.dispose();
    super.dispose();
  }
}
